﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;
using System.Collections.ObjectModel;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.ServiceModel.Dispatcher;

namespace CHPM.SL.TheService.Code
{
    public class InstanceProviderBehavior : IServiceBehavior
    {
        private readonly IInstanceProvider _instanceProvider;

        public InstanceProviderBehavior(IInstanceProvider instanceProvider)
        {
            this._instanceProvider = instanceProvider;
        }

        public void AddBindingParameters(
              ServiceDescription serviceDescription
            , ServiceHostBase serviceHostBase
            , Collection<ServiceEndpoint> endpoints
            , BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
            {                
                foreach (EndpointDispatcher endpointDispatcher in dispatcher.Endpoints)
                {
                    DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
                    dispatchRuntime.InstanceProvider = this._instanceProvider;                                        
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }

    }
}
